package com.ruoyi.project.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.domain.OrderDispatch;
import com.ruoyi.project.domain.OrderReport;
import com.ruoyi.project.mapper.OrderDispatchMapper;
import com.ruoyi.project.mapper.OrderReportMapper;
import com.ruoyi.project.service.IOrderDispatchService;

/**
 * 派单Service业务层处理
 * 
 * @author phil
 * @date 2020-05-31
 */
@Service
public class OrderDispatchServiceImpl implements IOrderDispatchService{
	@Autowired
	private OrderDispatchMapper orderDispatchMapper;
	@Autowired
	private OrderReportMapper orderReportMapper;

	/**
	 * 查询派单
	 * 
	 * @param orderId 派单ID
	 * @return 派单
	 */
	@Override
	public OrderDispatch selectOrderDispatchById(Long orderId){
		return orderDispatchMapper.selectOrderDispatchById(orderId);
	}

	/**
	 * 查询派单列表
	 * 
	 * @param orderDispatch 派单
	 * @return 派单
	 */
	@Override
	public List<OrderDispatch> selectOrderDispatchList(OrderDispatch orderDispatch){
		return orderDispatchMapper.selectOrderDispatchList(orderDispatch);
	}

	/**
	 * 新增派单
	 * 
	 * @param orderDispatch 派单
	 * @return 结果
	 */
	@Override
	public int insertOrderDispatch(OrderDispatch orderDispatch){
		orderDispatch.setCreateTime(DateUtils.getNowDate());
		return orderDispatchMapper.insertOrderDispatch(orderDispatch);
	}

	/**
	 * 修改派单
	 * 
	 * @param orderDispatch 派单
	 * @return 结果
	 */
	@Override
	public int updateOrderDispatch(OrderDispatch orderDispatch){
		orderDispatch.setUpdateTime(DateUtils.getNowDate());
		return orderDispatchMapper.updateOrderDispatch(orderDispatch);
	}

	/**
	 * 删除派单对象
	 * 
	 * @param ids 需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public int deleteOrderDispatchByIds(String ids){
		return orderDispatchMapper.deleteOrderDispatchByIds(Convert.toStrArray(ids));
	}

	/**
	 * 删除派单信息
	 * 
	 * @param orderId 派单ID
	 * @return 结果
	 */
	@Override
	public int deleteOrderDispatchById(Long orderId){
		return orderDispatchMapper.deleteOrderDispatchById(orderId);
	}

	@Override
	public List<Map<String, Object>> getMyOrderList(Map<String, Object> paramMap){
		Map<String, List<Map<String, Object>>> resultMap = new HashMap<>();
		List<Map<String, Object>> selectedList = orderDispatchMapper.getMyOrderList(paramMap);
		if(selectedList != null && selectedList.size() > 0){
			for(Map<String, Object> tempMap : selectedList){
				String customer = StringUtils.defaultString((String)tempMap.get("customer"));
				// 处理回款状态报告
				String orderId = tempMap.get("orderId").toString();
				OrderReport orderReport = new OrderReport();
				orderReport.setOrderId(Long.parseLong(orderId));
				List<OrderReport> orderReportList = orderReportMapper.selectOrderReportList(orderReport);
				if(orderReportList != null && orderReportList.size() > 0){
					tempMap.put("orderReport", orderReportList);
				}

				// 包含这个key,直接把当前内容加入到列表
				if(resultMap.containsKey(customer)){
					resultMap.get(customer).add(tempMap);
				}else{// 不包含就创建一个新List放入
					List<Map<String, Object>> newList = new ArrayList<Map<String, Object>>();
					newList.add(tempMap);
					resultMap.put(customer, newList);
				}
			}
		}

		// 数据格式化,先遍历Map取值，转换成Map
		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		if(!resultMap.isEmpty()){
			for(String key : resultMap.keySet()){
				Map<String, Object> tempMap = new HashMap<>();
				tempMap.put("customer", key);
				tempMap.put("list", resultMap.get(key));
				resultList.add(tempMap);
			}
		}
		return resultList;
	}
}
